package com.smy.jkds.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smy.jkds.po.Card;
import com.smy.jkds.po.JkdsUser;
import com.smy.jkds.po.TTaskRecord;
import com.smy.jkds.dao.TTaskRecordDAO;
import com.smy.jkds.service.CardService;
import com.smy.jkds.service.TTaskRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smy.jkds.vo.TaskCondition;
import com.smy.jkds.vo.TaskVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 任务记录表 服务实现类
 * </p>
 *
 * @author 01140
 * @since 2021-10-30
 */
@Service
public class TTaskRecordServiceImpl extends ServiceImpl<TTaskRecordDAO, TTaskRecord> implements TTaskRecordService {

    @Autowired
    private CardService cardService;

    @Override
    public List<TTaskRecord> queryTaskRecoordList(String userId, String createDate) {
        if (StringUtils.isAnyBlank(userId, createDate)){
            throw new RuntimeException("参数为空，请确认");
        }
        QueryWrapper<TTaskRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(TTaskRecord.ColumnEnum.user_id.name(), userId);
        queryWrapper.eq(TTaskRecord.ColumnEnum.create_date.name(), createDate);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public Integer getMaxCount(TaskCondition taskCondition) {
        return getBaseMapper().getMaxCount(taskCondition);
    }

    @Override
    public int batchInsert(List<TTaskRecord> taskRecords) {
        if (CollectionUtil.isEmpty(taskRecords)){
            return 0;
        }
        return getBaseMapper().batchInsert(taskRecords);
    }

    @Override
    public List<TaskVO> taskVOList(JkdsUser user, String createDate, String id) {
        TTaskRecord taskRecord = new TTaskRecord();
        taskRecord.setId(id);
        taskRecord.setUserId(user.getId());
        taskRecord.setCreateDate(createDate);
        QueryWrapper<TTaskRecord> queryWrapper = taskRecord.queryWrapper();
        queryWrapper.orderByDesc(TTaskRecord.ColumnEnum.create_time.name());
        List<TTaskRecord> tTaskRecordList =  this.list(queryWrapper);
        if (!CollectionUtils.isEmpty(tTaskRecordList)){
            return tTaskRecordList.stream().map( t ->{
                TaskVO vo = new TaskVO();
                vo.setId(t.getId());
                vo.setTaskTitle(t.getTitle());
                vo.setContent("");
                vo.setCardCount(0);
                vo.setCreateTime(t.getCreateTime());
                QueryWrapper<Card> cardQueryWrapper = new QueryWrapper();
                cardQueryWrapper.eq(Card.ColumnEnum.task_id.name(), t.getId());
                cardQueryWrapper.eq(Card.ColumnEnum.user_id.name(), t.getUserId());
                List<Card> cards = cardService.list(cardQueryWrapper);
                if (!CollectionUtils.isEmpty(cards)){
                    vo.setCardCount(cards.size());
                    vo.setContent(cards.get(0).getContent());
                    if (StringUtils.isNotBlank(cards.get(0).getTag())){
                        vo.setContent("#" + cards.get(0).getTag() + "#" + vo.getContent());
                    }
                }
                return vo;
            }).collect(Collectors.toList());
        }else {
            return null;
        }
    }
}
